﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FB.DataAccess;
using FB.Utilities;
using log4net;

namespace FB.Bussiness
{
    public class ChanelBO
    {
        private static readonly ILog log = LogManager.GetLogger("ChanelBO");
        public IEnumerable<Chanel> GetAllChanel()
        {
            ChanelDAO _dao = new ChanelDAO();
            return _dao.GetAllChanel();
        }
        public Chanel GetChanelById(Guid id)
        {
            ChanelDAO _dao = new ChanelDAO();
            return _dao.GetChanelByID(id);
        }
        public bool UpdateChanel(Chanel inChanel)
        {
            ChanelDAO _dao = new ChanelDAO();
            return _dao.UpdateChanel(inChanel);
        }
        public bool InsertChanel(Chanel inChanel)
        {
            ChanelDAO _dao = new ChanelDAO();
            return _dao.InsertChanel(inChanel);
        }
        public ReturnValue<bool> DelChanel(Guid id)
        {
            ChanelDAO _dao = new ChanelDAO();
            return _dao.DelChanel(id);
        }
        public ReturnValue<int> FeedChanel(Guid chanelID)
        {
            log.Info(string.Format("5. Feed chanel: {0}", chanelID));
            //Number of video are feed
            int VideoFeedNo = 0;
            // Get channel name
            Chanel FChanel = GetChanelById(chanelID);
            if (FChanel == null)
            {
                return new ReturnValue<int>(0, "Chanel is not exist in DB");
            }
            // Get list video
            List<YouTubeVideoObject> VideoList =  YouTubeVideoHelper.GetVideosFromChanel(FChanel.ID, FChanel.Name, FChanel.FeedIndex);
            if (VideoList != null)
            {
                // Insert list video to db
                VideoBO VBO = new VideoBO();
                foreach (YouTubeVideoObject v in VideoList)
                {
                    if (VBO.InsertVideo(new Video(v)).RetValue)
                    {
                        VideoFeedNo++;
                    }
                }

                // Update Chanel is feeded and feed date
                FChanel.LastFeedDate = DateTime.Now;
                FChanel.FeedIndex += ((VideoList.Count > 5) ? VideoList.Count - 5 : VideoList.Count);
                UpdateChanel(FChanel);
                log.Info(string.Format("Feed successful {0} videos from chanel {1}", VideoFeedNo, chanelID));
            }
            return new ReturnValue<int>(VideoFeedNo,"");
        }
        public void DoFeedChanelJob()
        {
            log.Info("4. Find a chanel to feed");
            //Get one chanel which have last feed more than 15 days to feed
            ChanelDAO CDAO = new ChanelDAO();
            Chanel FChanel = CDAO.GetNeed2FeedChanel();
            if (FChanel != null)
            {
                FeedChanel(FChanel.ID);
            }
            else
            {
                log.Info("4. Nothing to feed");
            }
        }
    }
}
